const db = require("../config/mysql_util");
class UsersModel {
  constructor() {}
  findById(user_id) {
    return db.query("select * from users where user_id = ?", [user_id]);
  }
  findByUsername(user_name) {
    return db.query("select * from users where user_name = ?", [user_name]);
  }
  login(data) {
    return db.query("select * from users where (user_name = ? or user_mobile = ? or user_email = ?) and user_pwd= ? and role = ?", [data.user_name, data.user_name, data.user_name, data.user_pwd, data.role]);
  }
  reg(data) {
    return db.query("insert into users set ?", [data]);
  }
  update(data, user_id) {
    return db.query("update users set ? where user_id = ?", [data, user_id]);
  }

  getCount(data) {
    let sql = ` select count(*) count from users where 1 `;
    if (data.user_name) sql += ` and (user_name like '%${data.user_name}%' or user_mobile like '%${data.user_name}%' or user_email like '%${data.user_name}%' or role like '%${data.user_name}%') `;
    if(data.role) sql += `  and role = '${data.role}' `
    return db.query(sql);
  }
  findUsersList(data) {
    let limit = data.pagesize * (data.page - 1);
    let sql = ` select * from users where 1 `;
    if (data.user_name) sql += ` and (user_name like '%${data.user_name}%' or user_mobile like '%${data.user_name}%' or user_email like '%${data.user_name}%' or role like '%${data.user_name}%') `;
    if(data.role) sql += `  and role = '${data.role}' `
    sql += ` limit ${limit}, ${data.pagesize} `;
    return db.query(sql);
  }
  delById(user_id){
    return db.query("delete from users where user_id = ?", [user_id]);
  }
}

module.exports = new UsersModel();